package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;


//不能用单例模式
public class DBUtils {
	private final static String URL = "jdbc:oracle:thin:@ZHA-ITA077-w7:1521:ita3"; 
	private final static String CLASS_NAME = "oracle.jdbc.OracleDriver"; 
	private final static String USER_NAME = "allen";
	private final static String PASSEORD = "123";
	
	private static BasicDataSource dataSource;
	
	static{
		dataSource = new BasicDataSource();
		dataSource.setUrl(URL);
		dataSource.setDriverClassName(CLASS_NAME);
		dataSource.setUsername(USER_NAME);
		dataSource.setPassword(PASSEORD);
		dataSource.setMaxActive(2);
	}
	
	public static Connection createConnection(){
		Connection conn = null;
		try {
			Class.forName(CLASS_NAME);
			conn = DriverManager.getConnection(URL,USER_NAME,PASSEORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
		try {
			if (rs!=null) {
				rs.close();
			}
			if (pst!=null) {
				pst.close();
			}
			if (conn!=null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public static Connection createConnectionWithDataSource(){
		Connection conn = null;
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	

}
